package com.yjhb.android.sbz.fragment.taskmanage;

import android.view.View;

import com.yjhb.android.sbz.entity.AddTask;
import com.yjhb.android.sbz.tool.EventMsg;
import com.yjhb.android.sbz.entity.ManagerTask;
import com.yjhb.android.sbz.enums.TaskState;
import com.yjhb.android.sbz.fragment.BaseListFragment;
import com.yjhb.android.sbz.mvp.managertasklist.ManagerTaskListPresenter;
import com.yjhb.android.sbz.mvp.managertasklist.ManagerTaskListView;
import com.jojo.android.zxlib.base.BaseRecyclerAdapter;
import com.jojo.android.zxlib.tool.ZxLogTool;
import com.jojo.android.zxlib.tool.ZxToast;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;


/**
 * 创建者 zx
 * 创建时间 2020/12/7 16:25
 */
public abstract class BaseTaskManageFragment<RA extends BaseRecyclerAdapter> extends BaseListFragment<ManagerTaskListPresenter, RA, ManagerTask>
        implements ManagerTaskListView {
    private final String TAG = "BaseTaskManageFragment";

    private int focusIndex;

    @Override
    protected void onLoadData(int size, int current) {
        if (presenter != null) {
            presenter.getTaskList(getState(), size, current);
        }
    }

    protected abstract Integer getState();

    @Override
    protected ManagerTaskListPresenter initPresenter() {
        return new ManagerTaskListPresenter(this);
    }

    @Override
    public void onUserVisibleChanged(boolean visible) {
        super.onUserVisibleChanged(visible);
        if (visible) {
            current = 1;
            if (loadingDialog != null) {
                loadingDialog.show();
            }
            onLoadData(size, current);
        }
    }

    @Override
    public void onItemClick(int index, Object o) {
        focusIndex = index;
    }

    @Override
    public void onViewClick(View view, String msg, int index, Object o) {
        focusIndex = index;
    }

    @Override
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMsg(EventMsg msg) {
        try {
            if (msg.msg == EventMsg.REQUEST_UPDATE_DATA_TASK_MANAGER_LIST) {
                if (msg.object instanceof TaskState) {
                    if (((TaskState) msg.object).getState() == getState()) {
                        initData();
                    }
                }
            }
            if (msg.msg == EventMsg.REQUEST_UPDATE_DATA_TASK_MANAGER_LIST_ITEM) {
                if (dataSet.size() > focusIndex)
                    presenter.loadTask(dataSet.get(focusIndex).getTask().getId());
            }
        } catch (Exception e) {
            ZxLogTool.err(e);
        }


    }

    @Override
    public void onRefreshData(List<ManagerTask> managerTasks) {
        loadingDialog.cancel();
        smartRefreshLayout.finishRefresh();
        this.dataSet.clear();
        if (managerTasks != null) {
            this.dataSet.addAll(managerTasks);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onLoadData(List<ManagerTask> managerTasks) {
        try {
            if (managerTasks != null) {
                this.dataSet.addAll(managerTasks);
                adapter.notifyDataSetChanged();
            }
        } catch (Exception e) {
            ZxLogTool.err(e);
        } finally {
            smartRefreshLayout.finishLoadmore();
        }
    }

//    @Override
//    public void onFailure(int code, String msg) {
//        loadingDialog.cancel();
//        smartRefreshLayout.finishLoadmore();
//        smartRefreshLayout.finishRefresh();
//        ZxToast.showBottomToastLight(msg);
//    }

    @Override
    public void onWait(String tips) {
        loadingDialog.setTipsText(tips);
        loadingDialog.show();
    }


    @Override
    public void onSuccess(String tips, String taskId) {
        loadingDialog.cancel();
        ZxToast.showSuccessBottomToast(tips);
        //操作成功后，更新这一项任务。
        presenter.loadTask(Integer.parseInt(taskId));
    }

    @Override
    public void onTask(ManagerTask managerTask) {
        try {
            int index = 0;
            for (int i = 0; i < dataSet.size(); i++) {
                if (dataSet.get(i).getTask().getId().equals(managerTask.getTask().getId())) {
                    index = i;
                    break;
                }
            }
            dataSet.remove(index);
            if (managerTask.getTask().getState() == getState()) {
                dataSet.add(index, managerTask);
                adapter.notifyItemChanged(index);
            } else {
                adapter.notifyDataSetChanged();
            }
        } catch (Exception e) {
            ZxLogTool.err(e);
        }
    }

    @Override
    public void onTaskDetail(AddTask addTask, ManagerTask managerTask) {

    }

    @Override
    public void onTaskDeleteSuccess() {
        loadingDialog.cancel();
        current = 1;
        onLoadData(size, current);
    }
}
